function s2h_handler(){
if(GM_safeVariables["ds_armyAssignmented_"+window.location.host+GM_getValue("ds_currentCityID")] != null && GM_safeVariables["ds_armyAssignmented_"+window.location.host+GM_getValue("ds_currentCityID")] != ''){
      log("軍隊分配已經完成,正在跳轉到首頁...");
      clickLink('/mindex');
      return;
    }

	//function 1
  var _setUIBySoldierInfo = function(soldierInfo){
    var lis = $x("//ul[@id='all_soldier']/li");
    for(var i=0;i<lis.length;i++){
      if(soldierInfo.name == lis[i].getAttribute("title")){
        lis[i].childNodes[1].innerHTML = soldierInfo.count.toString();
      }
    }
  }
  //funciton 2
  var _collectArmyInfo = function(){//collect army info
    var soldierInfos = []; //{name:"獅鷲",count:10,sysName:"s_1_3"};
    var lis = $x("//ul[@id='all_soldier']/li");
    for(var i=0;i<lis.length;i++){
      var soldierInfo = {};
      soldierInfo.name = lis[i].getAttribute("title");
      soldierInfo.count = parseInt(lis[i].childNodes[1].innerHTML);
      soldierInfo.sysName = lis[i].getElementsByTagName('input')[0].getAttribute("id");
      soldierInfos.push(soldierInfo);
    }
    return soldierInfos;
  }
	//function 3
  
   var handler_armyAssignmentPage_average = function(objState){
    //do army assignment
    var rules = CONFIG.army_rules;
    for(var i=0;i<rules.length;i++){ // loop for rules
      var rule = rules[i]; //{id:"1111",heroName:"虧托斯",armyType:"鬼魂",armyCount:100}
      //find hero 
      var heros = $x("//div[@class='hero_army']");
      var tarHero = null;
      for(var j=0;j<heros.length;j++){
        if(heros[j].getElementsByTagName("h3")[0].innerHTML.indexOf(rule.heroName)>=0)
          tarHero = heros[j];
      }
      if(tarHero == null) continue; // no hero match current rule,jump to next loop
      //clear all slots for current hero
      var liSlots = tarHero.getElementsByTagName('li');
      for(var j=0;j<liSlots.length;j++){
        if(liSlots[j].getAttribute('class') == 'on'){ // update exists army assignment.
          var link = liSlots[j].firstChild;
          var evt = document.createEvent("MouseEvents");  
          evt.initEvent("click",true,true);  
          link.dispatchEvent(evt);
        }
      }
      var soldierInfos = _collectArmyInfo();
      var soldierInfo = soldierInfos.getObjectByProperty("name",rule.armyType);//{name:"獅鷲",count:10,sysName:"s_1_3"};
      if(soldierInfo == null) continue; // no soldier match current rule,jump to next loop
      if(soldierInfo.count == 0) continue;
      var strCapacity = tarHero.getElementsByTagName("p")[0].childNodes[1].innerHTML;
      var heroCapacity = parseInt(strCapacity.split('/')[1]);
      var slotsCount = liSlots.length;
      var finalArmyCount = rule.armyCount;
      if(soldierInfo.count < finalArmyCount) finalArmyCount = soldierInfo.count;
      if(heroCapacity < finalArmyCount) finalArmyCount = heroCapacity;
      var averageCount = parseInt(finalArmyCount/slotsCount);
      var modCount = finalArmyCount%slotsCount;
      //do army assignment
      for(var j=0;j<liSlots.length;j++){
        var liSlot = liSlots[j];
        if(j<modCount){
          liSlot.childNodes[2].value = (averageCount+1).toString();
        }else{
          liSlot.childNodes[2].value = averageCount.toString();
        }
        liSlot.childNodes[1].setAttribute('title',soldierInfo.name);
        liSlot.childNodes[1].setAttribute('src','/sys/images/s/'+soldierInfo.sysName+'.gif');
        liSlot.childNodes[3].value = soldierInfo.sysName+'.gif';
        liSlot.setAttribute('class','on');
      }
      soldierInfo.count -= finalArmyCount;
      _setUIBySoldierInfo(soldierInfo);
    } // loop for rules

    //logic code for submit the assignments.
    GM_safeVariables["ds_armyAssignmented_"+window.location.host+GM_getValue("ds_currentCityID")]="true";
    GM_safeVariables["ds_lastArmyAssignmentedHour_"+window.location.host+GM_getValue("ds_currentCityID")]=(new Date().getHours().toString());
	    var randomTime = Math.random()*15000;
	    randomTime = randomTime<3000?3000:randomTime;
	    window.setTimeout(function(){
	      document.getElementById('fbbtn').click();
	    },randomTime);
  			
  }
	
	
  var handler_armyAssignmentPage = function(objState){
    /*if(GM_getValue("ds_armyAssignmented_"+window.location.host+GM_getValue("ds_currentCityID")) != null && GM_getValue("ds_armyAssignmented_"+window.location.host+GM_getValue("ds_currentCityID")) != ''){
      log("軍隊分配已經完成,正在跳轉到首頁...");
      clickLink('/mindex');
      return;
    }*/
    if(CONFIG.enable_armyRulesAverage = true){
      return handler_armyAssignmentPage_average(objState);
    }
    //collect army info
    var soldierInfos = []; //{name:"獅鷲",count:10,sysNae:"s_1_3"};
    var lis = $x("//ul[@id='all_soldier']/li");
    for(var i=0;i<lis.length;i++){
      var soldierInfo = {};
      soldierInfo.name = lis[i].getAttribute("title");
      soldierInfo.count = parseInt(lis[i].childNodes[1].innerHTML);
      soldierInfo.sysName = lis[i].getElementsByTagName('input')[0].getAttribute("id");
      soldierInfos.push(soldierInfo);
    }
    //do army assignment
    var rules = CONFIG.army_rules;
    for(var i=0;i<rules.length;i++){ // loop for rules
      var rule = rules[i]; //{id:"1111",heroName:"虧托斯",armyType:"鬼魂",armyCount:100}
      var soldierInfo = soldierInfos.getObjectByProperty("name",rule.armyType);
      if(soldierInfo == null) continue; // no soldier match current rule,jump to next loop
      if(soldierInfo.count == 0) continue;
      //find hero 
      var heros = $x("//div[@class='hero_army']");
      var tarHero = null;
      for(var j=0;j<heros.length;j++){
        if(heros[j].getElementsByTagName("h3")[0].innerHTML.indexOf(rule.heroName)>=0)
          tarHero = heros[j];
      }
      if(tarHero == null) continue; // no hero match current rule,jump to next loop
      //check hero army capacity
      var strCapacity = tarHero.getElementsByTagName("p")[0].childNodes[1].innerHTML;
      var currentNum = parseInt(strCapacity.split('/')[0]);
      var maxNum = parseInt(strCapacity.split('/')[1]);
      var freeNum = maxNum - currentNum;
      if(freeNum <= 0) continue; // user contains no free capacity for more army assignments.
      //check existing army slots for current rule
      var liSlots = tarHero.getElementsByTagName('li');
      for(var j=0;j<liSlots.length;j++){ // loop for target hero army slots
        var liSlot = liSlots[j];
        if(liSlot.getAttribute('class') == 'on'){ // update exists army assignment.
          var soldierName = liSlot.childNodes[1].getAttribute('title');
          if(soldierName != rule.armyType) continue; // soldierName not matched, into next slot loop
          var soldierCount = parseInt(liSlot.childNodes[2].value);
          if(soldierCount >= rule.armyCount) break; // soldierCount already match the rule's requirment,so just into next rule loop
          var needCount = 0;
          if(rule.armyCount - soldierCount >= freeNum){
            needCount = freeNum;
          }else{
            needCount = rule.armyCount - soldierCount;
          }
          if(currentNum + needCount > maxNum){
            needCount = maxNum - currentNum;
          }
          if(needCount >= soldierInfo.count){
            liSlot.childNodes[2].value = (soldierCount + soldierInfo.count).toString();
            soldierInfo.count = 0;
            tarHero.getElementsByTagName("p")[0].childNodes[1].innerHTML = (currentNum + soldierInfo.count).toString() + '/' + maxNum;
          }else{
            liSlot.childNodes[2].value = (soldierCount + needCount).toString();
            soldierInfo.count = soldierInfo.count - needCount;
            tarHero.getElementsByTagName("p")[0].childNodes[1].innerHTML = (currentNum + needCount).toString() + '/' + maxNum;
          }   
          break;       
        }else{ // add new army assignments;
          var needCount = 0;
          if(rule.armyCount > freeNum){
            needCount = freeNum;
          }else{
            needCount = rule.armyCount;
          }
          if(currentNum + needCount > maxNum){
            needCount = maxNum - currentNum;
          }
          if(needCount >= soldierInfo.count){
            liSlot.childNodes[2].value = soldierInfo.count.toString();
            soldierInfo.count = 0;
            tarHero.getElementsByTagName("p")[0].childNodes[1].innerHTML = (currentNum + soldierInfo.count).toString() + '/' + maxNum;
          }else{
            liSlot.childNodes[2].value = needCount.toString();
            soldierInfo.count = soldierInfo.count - needCount;
            tarHero.getElementsByTagName("p")[0].childNodes[1].innerHTML = (currentNum + needCount).toString() + '/' + maxNum;
          }
          liSlot.childNodes[1].setAttribute('title',soldierInfo.name);
          liSlot.childNodes[1].setAttribute('src','/sys/images/s/'+soldierInfo.sysName+'.gif');
          liSlot.childNodes[3].value = soldierInfo.sysName+'.gif';
          liSlot.setAttribute('class','on');
          break;
        }
        
      } // end loop for target hero army slots

    } // end loop for rules
    
    //logic code for submit the assignments.
   /* GM_setValue("ds_armyAssignmented_"+window.location.host+GM_getValue("ds_currentCityID"),"true");
    GM_setValue("ds_lastArmyAssignmentedHour_"+window.location.host+GM_getValue("ds_currentCityID"),(new Date().getHours().toString()));
	  */
	   GM_safeVariables["ds_armyAssignmented_"+window.location.host+GM_getValue("ds_currentCityID")]="true";
    GM_safeVariables["ds_lastArmyAssignmentedHour_"+window.location.host+GM_getValue("ds_currentCityID")]=(new Date().getHours().toString());
	  var randomTime = Math.random()*15000;
	    randomTime = randomTime<3000?3000:randomTime;
	    window.setTimeout(function(){
	      document.getElementById('fbbtn').click();
	    },randomTime);
  	
  }
  handler_armyAssignmentPage(state);
  
  
  
  
}


function s2h_handler_configMode(){
	
	 NewHtml = '<div><fieldSet ><legend>自動分兵:<input id="ds_cfg_btnClearRules" type="button" value="清空列表"/></legend>'+
                        '<div><input type="checkbox" id="ds_cfg_chkEnableArmyRules"/><label for="ds_cfg_chkEnableArmyRules">允許系統自動分兵(只有選中本項,自動分兵規則才能生效)</label></div>'+
		                    '<div>注意:自動分兵只在城內有英雄與閒兵時每小時進行一次.</div>'+
                        '<div><input type="checkbox" id="ds_cfg_chkEnableArmyRulesAverage"/><label for="ds_cfg_chkEnableArmyRulesAverage">把士兵平均分配到所有兵位</label></div>'+
		                    '<div id="ds_cfg_ctArmyRules"></div>'+
                        '<div>'+
                          '增加規則:英雄<input id="ds_cfg_iptHeroName" type="text" style="width:40px;"/>'+
                          '帶領士兵<input id="ds_cfg_iptArmyType" type="text" style="width:40px;"/>'+
                          '數量<input id="ds_cfg_iptArmyCount" type="text" style="width:40px;"/>'+
                          '<input id="ds_cfg_btnAddRule" type="button" value="+"/>'+
                        '</div>'+
                      '</fieldSet></div>';
    $("p[@class='note']").html(NewHtml);
   $("#ds_cfg_chkEnableArmyRules").get(0).checked = CONFIG.enable_armyRules;
   $("#ds_cfg_chkEnableArmyRulesAverage").get(0).checked = CONFIG.enable_armyRulesAverage;
   /*$("#ds_cfg_chkEnableAutoSellResource").get(0).checked = CONFIG.enable_autoSellResource;*/
   
   $("#ds_cfg_chkEnableArmyRules").click(function(){
	   	CONFIG.enable_armyRules = $("#ds_cfg_chkEnableArmyRules").get(0).checked;
   	});
   	
   	$("#ds_cfg_chkEnableArmyRulesAverage").click(function(){
	   	CONFIG.enable_armyRulesAverage = $("#ds_cfg_chkEnableArmyRulesAverage").get(0).checked;
   	});
   	
   	/*	CONFIG.enable_armyRulesAverage = $("#ds_cfg_chkEnableArmyRulesAverage").get(0).checked;
	   	CONFIG.enable_autoSellResource = $("#ds_cfg_chkEnableAutoSellResource").get(0).checked;
   */
    
	//code for process the army rules
   var createArmyRuleItem = function(ruleItem){
     var item = document.createElement("div");
     item.setAttribute("id",ruleItem.id);
     item.innerHTML = "<span style='margin-right:8px;'><strong style='margin-right:4px;margin-left:4px;'>" + ruleItem.heroName + "</strong>帶領<strong style='margin-right:4px;margin-left:4px;'>" + ruleItem.armyType + "</strong><strong style='margin-right:4px;margin-left:4px;'>" + ruleItem.armyCount + "</strong>名" + "</span>";
     var del = document.createElement("img");
     del.src = "/sys/images/del.gif";
     del.style.cursor = "pointer";
     //item.appendChild(del);
     item.insertBefore(del,item.firstChild);
     del.addEventListener('click',function(){
        $("#"+ruleItem.id).get(0).parentNode.removeChild($("#"+ruleItem.id).get(0));
        var rmIdx = -1;
        for(var i=0;i<CONFIG.army_rules.length;i++){
          if(CONFIG.army_rules[i].id == ruleItem.id){
            rmIdx = i;
            break;
          }
        }
        if(rmIdx != -1)
          CONFIG.army_rules.remove(rmIdx);
     },false);
     return item;
   }
   
   
   $('#ds_cfg_btnAddRule').get(0).addEventListener('click',function(){
     var id = new Date().getTime();
     var rule = new Object();
     rule.id = id;
     rule.heroName = $("#ds_cfg_iptHeroName").val();
     rule.armyType = $("#ds_cfg_iptArmyType").val();
     rule.armyCount = parseInt($("#ds_cfg_iptArmyCount").val());
     CONFIG.army_rules.push(rule);
     $("#ds_cfg_ctArmyRules").get(0).appendChild(createArmyRuleItem(rule));
   },false);
   
   
   $('#ds_cfg_btnClearRules').get(0).addEventListener('click',function(){
     CONFIG.army_rules = new Array();
     $("#ds_cfg_ctArmyRules").html('');
   },false);
   
   //restore the army rules list
   for(var i = 0;i<CONFIG.army_rules.length;i++){
     var armyRule = CONFIG.army_rules[i];
     $("#ds_cfg_ctArmyRules").get(0).appendChild(createArmyRuleItem(armyRule));
   }
}